package com.yanjue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yanjue.common.entity.IndexBooklist;
import com.yanjue.common.entity.IndexBooklistItem;
import com.yanjue.common.redisKey.RedisExpire;
import com.yanjue.common.redisKey.RedisHomepageKey;
import com.yanjue.common.redisKey.RedisService;
import com.yanjue.common.util.Result;
import com.yanjue.common.util.ResultUtil;
import com.yanjue.mapper.IndexBooklistItemMapper;
import com.yanjue.service.IndexBooklistItemService;
import com.yanjue.vo.BookVO;
import com.yanjue.vo.BooklistBookVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 *书单图书接口
 */

@RestController
@Api("精品页书单图书接口")
public class IndexBooklistItemController {
    @Autowired
    private IndexBooklistItemService indexBooklistItemService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private IndexBooklistItemMapper indexBooklistItemMapper;

    @ApiOperation(value = "书单更多分页接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "booklistId", value = "书单ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "page", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit", value = "每页数量", required = true, dataType = "Integer")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "", response = BookVO.class)})
    @GetMapping("index/getBooklistPagingBooks")
    public Result getBooklistPagingBooks(Integer booklistId, Integer page, Integer limit) {
        //直接在控制器里写分页 + redis
        //1.获取key
        String key = RedisHomepageKey.getBooklistItemPagingKey(booklistId);
        //2.将hash的key设置成页码 + limit，获取缓存
        String field = page.toString() + limit;
        List<BooklistBookVO> list = this.redisService.getHashListVal(key, field, BooklistBookVO.class);
        if (ObjectUtils.isEmpty(list)){
            //3.缓存中没有，读数据库
            list = new ArrayList<>();
            com.baomidou.mybatisplus.extension.plugins.pagination.Page<IndexBooklistItem> pageRequset = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page,limit);
            LambdaQueryWrapper<IndexBooklistItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(IndexBooklistItem::getBooklistId,booklistId);

            //4.构造条件构造器
            Page<IndexBooklistItem> pageInfo = indexBooklistItemMapper.selectPage(pageRequset, lambdaQueryWrapper);
            List<IndexBooklistItem> items = pageInfo.getRecords();

            //5.转化结果并缓存到redis
            for (IndexBooklistItem item : items) {
                String bookId = item.getBookId();
                BooklistBookVO vo = indexBooklistItemService.getBookVO(bookId);
                if (vo != null){
                    list.add(vo);
                }
            }
            //6.写入redis缓存
            if (!list.isEmpty()){
                this.redisService.setHashValExpire(key,field,list, RedisExpire.HOUR_TWO);
            }
        }

        return ResultUtil.success(list);
        //return this.indexBooklistItemService.getBooklistPagingBooks(booklistId, page, limit);

    }



    @ApiOperation(value = "书单换一换接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "booklistId", value = "书单ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "clientRandomNumber", value = "客户端当前书单编号", required = true, dataType = "Integer")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "", response = BookVO.class)})
    @GetMapping("index/getBooklistExchange")
    public Result getBooklistExchange(Integer booklistId, Integer clientRandomNumber) {

        return this.indexBooklistItemService.getBooklistExchange(booklistId, clientRandomNumber);
    }


}
